package com.school.system.controller;

import com.school.system.entity.SchoolClass;
import com.school.system.service.SchoolClassService;
import com.school.system.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级控制器
 * 
 * @author 鹿与兔
 * @since 2025-05-22
 */
@RestController
@RequestMapping("/classes")
@CrossOrigin
public class SchoolClassController {

    @Autowired
    private SchoolClassService schoolClassService;

    /**
     * 分页查询班级列表
     */
    @GetMapping
    public Result<Page<SchoolClass>> getClassPage(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        
        Pageable pageable = PageRequest.of(page, size);
        Page<SchoolClass> classPage = schoolClassService.getClassPage(pageable);
        return Result.success(classPage);
    }

    /**
     * 获取所有班级列表
     */
    @GetMapping("/all")
    public Result<List<SchoolClass>> getAllClasses() {
        List<SchoolClass> classes = schoolClassService.getAllClasses();
        return Result.success(classes);
    }

    /**
     * 根据ID获取班级信息
     */
    @GetMapping("/{id}")
    public Result<SchoolClass> getClassById(@PathVariable Long id) {
        SchoolClass schoolClass = schoolClassService.getClassById(id);
        if (schoolClass != null) {
            return Result.success(schoolClass);
        } else {
            return Result.error("班级不存在");
        }
    }

    /**
     * 添加班级
     */
    @PostMapping
    public Result<SchoolClass> addClass(@RequestBody SchoolClass schoolClass) {
        try {
            SchoolClass savedClass = schoolClassService.addClass(schoolClass);
            return Result.success("添加班级成功", savedClass);
        } catch (Exception e) {
            return Result.error("添加班级失败: " + e.getMessage());
        }
    }

    /**
     * 更新班级信息
     */
    @PutMapping("/{id}")
    public Result<SchoolClass> updateClass(@PathVariable Long id, @RequestBody SchoolClass schoolClass) {
        try {
            schoolClass.setId(id);
            SchoolClass updatedClass = schoolClassService.updateClass(schoolClass);
            return Result.success("更新班级信息成功", updatedClass);
        } catch (Exception e) {
            return Result.error("更新班级信息失败: " + e.getMessage());
        }
    }

    /**
     * 删除班级
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteClass(@PathVariable Long id) {
        try {
            schoolClassService.deleteClass(id);
            return Result.success("删除班级成功");
        } catch (Exception e) {
            return Result.error("删除班级失败: " + e.getMessage());
        }
    }

    /**
     * 根据年级获取班级列表
     */
    @GetMapping("/grade/{grade}")
    public Result<List<SchoolClass>> getClassesByGrade(@PathVariable String grade) {
        List<SchoolClass> classes = schoolClassService.getClassesByGrade(grade);
        return Result.success(classes);
    }

    /**
     * 根据班级名称搜索班级
     */
    @GetMapping("/search")
    public Result<List<SchoolClass>> searchClasses(@RequestParam String className) {
        List<SchoolClass> classes = schoolClassService.searchClassesByName(className);
        return Result.success(classes);
    }
}
